package com.spsy.system.service.impl;

import java.util.List;
import com.spsy.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.spsy.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.spsy.system.domain.RuleAttrInfo;
import com.spsy.system.mapper.RuleInfoMapper;
import com.spsy.system.domain.RuleInfo;
import com.spsy.system.service.IRuleInfoService;

/**
 * 商品规格Service业务层处理
 * 
 * @author arhat
 * @date 2024-11-15
 */
@Service
public class RuleInfoServiceImpl implements IRuleInfoService 
{
    @Autowired
    private RuleInfoMapper ruleInfoMapper;

    /**
     * 查询商品规格
     * 
     * @param id 商品规格主键
     * @return 商品规格
     */
    @Override
    public RuleInfo selectRuleInfoById(Long id)
    {
        return ruleInfoMapper.selectRuleInfoById(id);
    }

    /**
     * 查询商品规格列表
     * 
     * @param ruleInfo 商品规格
     * @return 商品规格
     */
    @Override
    public List<RuleInfo> selectRuleInfoList(RuleInfo ruleInfo)
    {
        return ruleInfoMapper.selectRuleInfoList(ruleInfo);
    }

    /**
     * 新增商品规格
     * 
     * @param ruleInfo 商品规格
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRuleInfo(RuleInfo ruleInfo)
    {
        ruleInfo.setCreateTime(DateUtils.getNowDate());
        int rows = ruleInfoMapper.insertRuleInfo(ruleInfo);
        insertRuleAttrInfo(ruleInfo);
        return rows;
    }

    /**
     * 修改商品规格
     * 
     * @param ruleInfo 商品规格
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRuleInfo(RuleInfo ruleInfo)
    {
        ruleInfo.setUpdateTime(DateUtils.getNowDate());
        ruleInfoMapper.deleteRuleAttrInfoByRuleId(ruleInfo.getId());
        insertRuleAttrInfo(ruleInfo);
        return ruleInfoMapper.updateRuleInfo(ruleInfo);
    }

    /**
     * 批量删除商品规格
     * 
     * @param ids 需要删除的商品规格主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRuleInfoByIds(Long[] ids)
    {
        ruleInfoMapper.deleteRuleAttrInfoByRuleIds(ids);
        return ruleInfoMapper.deleteRuleInfoByIds(ids);
    }

    /**
     * 删除商品规格信息
     * 
     * @param id 商品规格主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRuleInfoById(Long id)
    {
        ruleInfoMapper.deleteRuleAttrInfoByRuleId(id);
        return ruleInfoMapper.deleteRuleInfoById(id);
    }

    /**
     * 新增规格详情信息
     * 
     * @param ruleInfo 商品规格对象
     */
    public void insertRuleAttrInfo(RuleInfo ruleInfo)
    {
        List<RuleAttrInfo> ruleAttrInfoList = ruleInfo.getRuleAttrInfoList();
        Long id = ruleInfo.getId();
        if (StringUtils.isNotNull(ruleAttrInfoList))
        {
            List<RuleAttrInfo> list = new ArrayList<RuleAttrInfo>();
            for (RuleAttrInfo ruleAttrInfo : ruleAttrInfoList)
            {
                ruleAttrInfo.setRuleId(id);
                list.add(ruleAttrInfo);
            }
            if (list.size() > 0)
            {
                ruleInfoMapper.batchRuleAttrInfo(list);
            }
        }
    }
}
